#include "seismicgridmodel.h"

SeismicGridModel::SeismicGridModel(SeismicGrid* pt_grid, QObject *parent)
	: QAbstractTableModel(parent)
{
	m_pt_grid = pt_grid;
	fillHeaderModelVertical(m_verticalHeaderModel);
	fillHeaderModelHorizontal(m_horizontalHeaderModel);
}

int SeismicGridModel::rowCount(const QModelIndex &parent) const
{
	return 5;
}

int SeismicGridModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
	return 27;
}

QVariant SeismicGridModel::data(const QModelIndex &index, int role) const
{
	/*
	if (!index.isValid())
		 return QVariant();

	if (index.row() < 0)
		 return QVariant();

	if (index.row() >= 5)
		 return QVariant();

	if (index.column() < 0)
		 return QVariant();

	if (index.column() >= 27)
		 return QVariant();
*/
	SeismicPoint p;
	if (index.row()==0) p = m_pt_grid->P1;
	if (index.row()==1) p = m_pt_grid->P2;
	if (index.row()==2) p = m_pt_grid->P3;
	if (index.row()==3) p = m_pt_grid->P4;	
	if (index.row()==4) p = m_pt_grid->site;
	if (role == Qt::DisplayRole)
	{
		if (!p.isComplete()) {return QVariant("---");}
		QString str;
		switch (index.column())
		{
			case 0:
			{
				str.setNum(p.get_SpecParam(1).agn(),'f',3);
				return QVariant(str);
			}
			case 1:
			{
				str.setNum(p.get_SpecParam(1).Fo(),'f',3);
				return QVariant(str);
			}
			case 2:
			{
				str.setNum(p.get_SpecParam(1).Tsc(),'f',3);
				return QVariant(str);
			}
			case 3:
			{
				str.setNum(p.get_SpecParam(2).agn(),'f',3);
				return QVariant(str);
			}
			case 4:
			{
				str.setNum(p.get_SpecParam(2).Fo(),'f',3);
				return QVariant(str);
			}
			case 5:
			{
				str.setNum(p.get_SpecParam(2).Tsc(),'f',3);
				return QVariant(str);
			}
			case 6:
			{
				str.setNum(p.get_SpecParam(3).agn(),'f',3);
				return QVariant(str);
			}
			case 7:
			{
				str.setNum(p.get_SpecParam(3).Fo(),'f',3);
				return QVariant(str);
			}
			case 8:
			{
				str.setNum(p.get_SpecParam(3).Tsc(),'f',3);
				return QVariant(str);
			}
			case 9:
			{
				str.setNum(p.get_SpecParam(4).agn(),'f',3);
				return QVariant(str);
			}
			case 10:
			{
				str.setNum(p.get_SpecParam(4).Fo(),'f',3);
				return QVariant(str);
			}
			case 11:
			{
				str.setNum(p.get_SpecParam(4).Tsc(),'f',3);
				return QVariant(str);
			}
			case 12:
			{
				str.setNum(p.get_SpecParam(5).agn(),'f',3);
				return QVariant(str);
			}
			case 13:
			{
				str.setNum(p.get_SpecParam(5).Fo(),'f',3);
				return QVariant(str);
			}
			case 14:
			{
				str.setNum(p.get_SpecParam(5).Tsc(),'f',3);
				return QVariant(str);
			}
			case 15:
			{
				str.setNum(p.get_SpecParam(6).agn(),'f',3);
				return QVariant(str);
			}
			case 16:
			{
				str.setNum(p.get_SpecParam(6).Fo(),'f',3);
				return QVariant(str);
			}
			case 17:
			{
				str.setNum(p.get_SpecParam(6).Tsc(),'f',3);
				return QVariant(str);
			}
			case 18:
			{
				str.setNum(p.get_SpecParam(7).agn(),'f',3);
				return QVariant(str);
			}
			case 19:
			{
				str.setNum(p.get_SpecParam(7).Fo(),'f',3);
				return QVariant(str);
			}
			case 20:
			{
				str.setNum(p.get_SpecParam(7).Tsc(),'f',3);
				return QVariant(str);
			}
			case 21:
			{
				str.setNum(p.get_SpecParam(8).agn(),'f',3);
				return QVariant(str);
			}
			case 22:
			{
				str.setNum(p.get_SpecParam(8).Fo(),'f',3);
				return QVariant(str);
			}
			case 23:
			{
				str.setNum(p.get_SpecParam(8).Tsc(),'f',3);
				return QVariant(str);
			}
			case 24:
			{
				str.setNum(p.get_SpecParam(9).agn(),'f',3);
				return QVariant(str);
			}
			case 25:
			{
				str.setNum(p.get_SpecParam(9).Fo(),'f',3);
				return QVariant(str);
			}
			case 26:
			{
				str.setNum(p.get_SpecParam(9).Tsc(),'f',3);
				return QVariant(str);
			}
		}
		return QVariant();
	}
	/****************************************************************/
	/*                 HEADER CON SPANNING E MULTIRIGA              */
	/****************************************************************/
	if(role==HierarchicalHeaderView::HorizontalHeaderDataRole)
	{
		QVariant v;
		v.setValue((QObject*)& m_horizontalHeaderModel);
		return v;
	}
	if(role==HierarchicalHeaderView::VerticalHeaderDataRole)
	{
		QVariant v;
		v.setValue((QObject*)& m_verticalHeaderModel);
		return v;
	}
	/****************************************************************/
	if (index.row() == 4 && index.column() >= 0 && index.column() <=27 && role == Qt::TextColorRole)
		return QVariant(Qt::red);
	else if (index.column() >= 0 && index.column() <=27 && role == Qt::TextAlignmentRole)
		return QVariant(Qt::AlignCenter|Qt::AlignVCenter);
	else return QVariant();
}

bool SeismicGridModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if ( /*index.isValid() && */ role == Qt::EditRole){
//        QModelIndex i = this->index(0,0);
//        QModelIndex j = this->index(5,27);
//        emit(dataChanged(i,j));
          emit(dataChanged(index, index)); // necessario per aggiornare la vista
	}
	else return false;
	return true;
}

void SeismicGridModel::fillHeaderModelHorizontal(QStandardItemModel& headerModel)
{
	QList<QStandardItem*> l1;
	l1.push_back(new QStandardItem("agn"));
	l1.push_back(new QStandardItem("Fo"));
	l1.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l2;
	l2.push_back(new QStandardItem("agn"));
	l2.push_back(new QStandardItem("Fo"));
	l2.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l3;
	l3.push_back(new QStandardItem("agn"));
	l3.push_back(new QStandardItem("Fo"));
	l3.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l4;
	l4.push_back(new QStandardItem("agn"));
	l4.push_back(new QStandardItem("Fo"));
	l4.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l5;
	l5.push_back(new QStandardItem("agn"));
	l5.push_back(new QStandardItem("Fo"));
	l5.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l6;
	l6.push_back(new QStandardItem("agn"));
	l6.push_back(new QStandardItem("Fo"));
	l6.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l7;
	l7.push_back(new QStandardItem("agn"));
	l7.push_back(new QStandardItem("Fo"));
	l7.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l8;
	l8.push_back(new QStandardItem("agn"));
	l8.push_back(new QStandardItem("Fo"));
	l8.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l9;
	l9.push_back(new QStandardItem("agn"));
	l9.push_back(new QStandardItem("Fo"));
	l9.push_back(new QStandardItem("T*c"));

	QList<QStandardItem*> l_root;
	l_root.push_back(new QStandardItem("Tr = 30"));
	l_root.push_back(new QStandardItem("Tr = 50"));
	l_root.push_back(new QStandardItem("Tr = 72"));
	l_root.push_back(new QStandardItem("Tr = 101"));
	l_root.push_back(new QStandardItem("Tr = 140"));
	l_root.push_back(new QStandardItem("Tr = 201"));
	l_root.push_back(new QStandardItem("Tr = 475"));
	l_root.push_back(new QStandardItem("Tr = 975"));
	l_root.push_back(new QStandardItem("Tr = 2475"));

	l_root.at(0)->appendRow(l1);
	l_root.at(1)->appendRow(l2);
	l_root.at(2)->appendRow(l3);
	l_root.at(3)->appendRow(l4);
	l_root.at(4)->appendRow(l5);
	l_root.at(5)->appendRow(l6);
	l_root.at(6)->appendRow(l7);
	l_root.at(7)->appendRow(l8);
	l_root.at(8)->appendRow(l9);
	headerModel.insertRow(0,l_root);
}

void SeismicGridModel::fillHeaderModelVertical(QStandardItemModel& headerModel)
{
	QList<QStandardItem*> l;

    l.push_back(new QStandardItem("P1"));
    l.push_back(new QStandardItem("P2"));
    l.push_back(new QStandardItem("P3"));
    l.push_back(new QStandardItem("P4"));
	l.push_back(new QStandardItem("Sito"));

	headerModel.insertRow(0,l);;
}

void SeismicGridModel::updateWEDChange(void)
{
    reset();
    emit( dataChanged(QModelIndex(),QModelIndex()) );
}

